package io.renren.modules.project.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.DateUtils;
import io.renren.modules.project.dao.ChartOutputDao;
import io.renren.modules.project.service.ChartOutputService;
import io.renren.modules.project.vo.ChartOutputVoEntity;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Service("ChartOutputService")
public class ChartOutputServiceImpl extends ServiceImpl<ChartOutputDao, ChartOutputVoEntity> implements ChartOutputService {

    @Override
    public List<ChartOutputVoEntity> queryList(Map<String, Object> params){
        List<ChartOutputVoEntity> list = new ArrayList<>();
        if (params.get("sidx").toString().equals("work")) {
            list = baseMapper.getWorkOutputVoList( params );
        } else if (params.get("sidx").toString().equals("quality")) {
            list = baseMapper.getQualityOutputVoList( params );
        }

        return list;
    }

    @Override
    public HSSFWorkbook exportExcel(Map<String, Object> params){
        List<ChartOutputVoEntity> list = this.queryList(params);

        DecimalFormat decimalFormat=new DecimalFormat(".00");

        String startDate = params.get("startDate").toString();
        String endDate = params.get("endDate").toString();
        String sidx = params.get("sidx").toString();
        // 标题名称
        Calendar sDate = Calendar.getInstance();
        Calendar eDate = Calendar.getInstance();
        sDate.setTime( DateUtils.stringToDate(startDate, DateUtils.DATE_PATTERN) );
        eDate.setTime( DateUtils.stringToDate(endDate ,DateUtils.DATE_PATTERN) );
        String title = startDate.split("-")[0] + "年" + startDate.split("-")[1] + "月";
        if ( sDate.get(Calendar.YEAR) == eDate.get(Calendar.YEAR) && sDate.get(Calendar.MONTH) == ( eDate.get(Calendar.MONTH) - 1)){
            title += "";
        } else {
            eDate.add(Calendar.MONTH, -1);
            eDate.set(Calendar.DAY_OF_MONTH, 1);
            title += "至" + eDate.get(Calendar.YEAR) + "年" + String.valueOf( eDate.get(Calendar.MONTH ) + 1) + "月";
        }
        title +=  "产值统计表"  + (sidx.equals("work") ? "(作业组)" : "(质检组)");
        params.put("title",title);

        //声明一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        int rowIndex = 0;

        //生成一个表格，设置表格名称
        HSSFSheet sheet = workbook.createSheet("sheet1");
        // 表头
        String[] titleWork = new String[]{"项目名称","项目负责人","项目启动时间","作业完成时间","实际产值","备注"};
        String[] titleQuality = new String[]{"项目名称","项目负责人","项目启动时间","质检完成时间","质检产值","是否外检","备注"};

        //设置表格列宽度
        sheet.setColumnWidth(0, 180 * 100);
        sheet.setColumnWidth(1, 40 * 100);
        sheet.setColumnWidth(2, 40 * 100);
        sheet.setColumnWidth(3, 40 * 100);
        sheet.setColumnWidth(4, 40 * 100);
        sheet.setColumnWidth(5, 40 * 100);
        sheet.setColumnWidth(6, 200 * 100);
        // 标题字体设置
        HSSFFont hfont = workbook.createFont();
        hfont.setFontName("黑体");
        hfont.setFontHeightInPoints((short) 16);
        hfont.setBold(true);   //字体加粗
        // 标题表格样式
        HSSFCellStyle hstyle = workbook.createCellStyle();
        hstyle.setFont(hfont);
        hstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  // 垂直居中
        hstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);       // 水平居中


        // 标题行
        HSSFRow hRow = sheet.createRow(rowIndex++);
        hRow.setHeight((short) 540);
        HSSFCell hcell = hRow.createCell(0);
        hcell.setCellValue(new HSSFRichTextString(title));
        hcell.setCellStyle(hstyle);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));

        // 字段字体设置
        HSSFFont tfont = workbook.createFont();
        tfont.setFontName("宋体");
        tfont.setFontHeightInPoints((short) 12);
        tfont.setBold(true);   //字体加粗
        // 字段表格样式
        HSSFCellStyle tstyle = workbook.createCellStyle();
        tstyle.setFont(tfont);
        tstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  // 垂直居中
        // 红色字体
        HSSFFont rfont = workbook.createFont();
        rfont.setColor(Font.COLOR_RED);
        HSSFCellStyle rstyle = workbook.createCellStyle();
        rstyle.setFont(rfont);


        // 字段行
        HSSFRow tRow = sheet.createRow(rowIndex++);
        tRow.setHeight((short) 410);
        HSSFCell tcell;
        if(sidx.equals("work")) {
            for (int i=0; i< titleWork.length; i++) {
                tcell = tRow.createCell(i);
                tcell.setCellValue(new HSSFRichTextString(titleWork[i]));
                tcell.setCellStyle(tstyle);
            }
        }else {
            for (int i=0; i< titleQuality.length; i++) {
                tcell = tRow.createCell(i);
                tcell.setCellValue(new HSSFRichTextString(titleQuality[i]));
                tcell.setCellStyle(tstyle);
            }
        }

        // 各队明细产值
        // 工作组名字体设置
        HSSFFont gfont = workbook.createFont();
        gfont.setFontName("宋体");
        gfont.setFontHeightInPoints((short) 9);
        gfont.setBold(true);   //字体加粗
        HSSFCellStyle gstyle = workbook.createCellStyle();
        gstyle.setFont(tfont);
        gstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  // 垂直居中


        Long groupId = 0L; //初始工作组Id
        String groupNameTemp = ""; // 临时工作组
        String qualityUsecount = ""; // 质检人员账号
        String qualityUserName = ""; // 质检人员姓名
        int size = 0;
        float output = 0;
        for (ChartOutputVoEntity voEntity : list) {
            // 工作组和质检组的报告
            if ( (voEntity.getgroupId() != groupId && sidx.equals("work")) ||
                    ( ! (voEntity.getQualityUseraccount() != null && voEntity.getQualityUseraccount().equals(qualityUsecount) )  && !sidx.equals("work")) ) {
                groupId = voEntity.getgroupId();
                qualityUsecount = voEntity.getQualityUseraccount();
                if (size == 0 && output == 0) {
                    // 写入工作组名
                    sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 0, 4));
                    HSSFRow gRow = sheet.createRow(rowIndex++);
                    gRow.setHeight((short) 300);
                    HSSFCell gcell = gRow.createCell(0);
                    gcell.setCellValue(new HSSFRichTextString( sidx.equals("work")? voEntity.getgroupName(): voEntity.getQualityUsername()));
                    gcell.setCellStyle(gstyle);
                    groupNameTemp = voEntity.getgroupName();
                    qualityUserName = voEntity.getQualityUsername();
                } else {
                    // 写入合计产值
                    sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 0, 3));
                    HSSFRow outRow = sheet.createRow(rowIndex++);
                    outRow.setHeight((short) 300);
                    HSSFCell outcell;
                    outcell = outRow.createCell(0);outcell.setCellValue(new HSSFRichTextString( (sidx.equals("work") ? groupNameTemp :qualityUserName)  + ": 合计" + String.valueOf(size) + "个项目"));outcell.setCellStyle(gstyle);
                    outcell = outRow.createCell(4);outcell.setCellValue(new HSSFRichTextString( decimalFormat.format(output) ));outcell.setCellStyle(gstyle);
                    output = 0;
                    size = 0;
                    groupId = voEntity.getgroupId();
                    groupNameTemp = voEntity.getgroupName();
                    qualityUserName = voEntity.getQualityUsername();
                    // 写入工作组名
                    sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 0, 4));
                    HSSFRow gRow = sheet.createRow(rowIndex++);
                    gRow.setHeight((short) 300);
                    HSSFCell gcell = gRow.createCell(0);
                    gcell.setCellValue(new HSSFRichTextString(sidx.equals("work")? voEntity.getgroupName(): voEntity.getQualityUsername()));
                    gcell.setCellStyle(gstyle);

                }
            }
            size++;
            output += voEntity.getprojectActuallyOutput();
            HSSFRow proRow = sheet.createRow(rowIndex++);
            HSSFCell procell;
            procell = proRow.createCell(0);
            procell.setCellValue(new HSSFRichTextString(voEntity.getprojectName()));
            procell = proRow.createCell(1);
            procell.setCellValue(new HSSFRichTextString(voEntity.getProjectCharge()));
            if (sidx.equals("quality")) {
                procell = proRow.createCell(2);
                procell.setCellValue(new HSSFRichTextString( voEntity.getprojectStartDateTime() == null? "":DateUtils.format(voEntity.getprojectStartDateTime())));
                procell = proRow.createCell(3);
                procell.setCellValue(new HSSFRichTextString(voEntity.getqFinishDateTime() == null? "":DateUtils.format(voEntity.getqFinishDateTime())));
                procell = proRow.createCell(4);
                procell.setCellValue(new HSSFRichTextString(voEntity.getprojectActuallyOutput().toString()));
                procell = proRow.createCell(5);
                procell.setCellValue(new HSSFRichTextString( (voEntity.getOutsideCheck() != null && voEntity.getOutsideCheck() == 1) ? "是": "否"));
                if (voEntity.getOutsideCheck() != null && voEntity.getOutsideCheck() == 1) {
                    procell.setCellStyle(rstyle);
                }
                procell = proRow.createCell(6);
                procell.setCellValue(new HSSFRichTextString(voEntity.getOutputRemark()));
            } else {
                procell = proRow.createCell(2);
                procell.setCellValue(new HSSFRichTextString( voEntity.getprojectStartDateTime() == null? "":DateUtils.format(voEntity.getprojectStartDateTime())));
                procell = proRow.createCell(3);
                procell.setCellValue(new HSSFRichTextString( voEntity.getwFinishDateTime() == null? "":DateUtils.format(voEntity.getwFinishDateTime())));
                procell = proRow.createCell(4);
                procell.setCellValue(new HSSFRichTextString(voEntity.getprojectActuallyOutput().toString()));
                procell = proRow.createCell(5);
                procell.setCellValue(new HSSFRichTextString(voEntity.getOutputRemark()));
            }
        }
        if(output != 0 && (sidx.equals("work") ? !groupNameTemp.equals("") :  !qualityUserName.equals("")) ){
            // 写入合计产值
            sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 0, 3));
            HSSFRow outRow = sheet.createRow(rowIndex++);
            outRow.setHeight((short) 300);
            HSSFCell outcell;
            outcell = outRow.createCell(0);outcell.setCellValue(new HSSFRichTextString( (sidx.equals("work") ? groupNameTemp : qualityUserName) + ": 合计" + String.valueOf(size) + "个项目"));outcell.setCellStyle(tstyle);
            outcell = outRow.createCell(4);outcell.setCellValue(new HSSFRichTextString( decimalFormat.format(output) ));outcell.setCellStyle(tstyle);
        }
        return workbook;
    }
}
